//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "DiscretizedConvertible.h"
using namespace Cephei::QL::Experimental;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Lattice.h>
#include <gen/QL/DiscretizedAsset.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Processes;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::CDiscretizedConvertible::CDiscretizedConvertible (boost::shared_ptr<QuantLib::DiscretizedConvertible>& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedConvertible::typeid)
{
#ifdef HANDLE
	_phDiscretizedConvertible = NULL;
#endif
	_ppDiscretizedConvertible = &childNative;
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedConvertible));
}
Cephei::QL::Experimental::CDiscretizedConvertible::CDiscretizedConvertible (QuantLib::DiscretizedConvertible& childNative, Object^ owner) : CDiscretizedAsset(CDiscretizedConvertible::typeid)
{
#ifdef HANDLE
	_phDiscretizedConvertible = NULL;
#endif
	_ppDiscretizedConvertible = new boost::shared_ptr<QuantLib::DiscretizedConvertible> (&childNative);
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedConvertible));
    _DiscretizedConvertibleOwner = owner;
    _DiscretizedAssetOwner = owner;
}

Cephei::QL::Experimental::CDiscretizedConvertible::CDiscretizedConvertible (CDiscretizedConvertible^ copy) : CDiscretizedAsset(CDiscretizedConvertible::typeid)
{
#ifdef HANDLE
	_phDiscretizedConvertible = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppDiscretizedConvertible = new boost::shared_ptr<QuantLib::DiscretizedConvertible> (copy->GetShared());
        _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedConvertible));
    }
}
Cephei::QL::Experimental::CDiscretizedConvertible::CDiscretizedConvertible (System::Type^ t) : CDiscretizedAsset(CDiscretizedConvertible::typeid)
{
#ifdef HANDLE
	_phDiscretizedConvertible = NULL;
#endif
	if (!t->IsSubclassOf(CDiscretizedConvertible::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::CDiscretizedConvertible::CDiscretizedConvertible (QuantLib::Handle<QuantLib::DiscretizedConvertible>& childNative, Object^ owner)  : CDiscretizedAsset(CDiscretizedConvertible::typeid)
{
	_phDiscretizedConvertible = &childNative;
	_ppDiscretizedConvertible = &static_cast<boost::shared_ptr<QuantLib::DiscretizedConvertible>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedConvertible));
    _DiscretizedConvertibleOwner = owner;
}
Cephei::QL::Experimental::CDiscretizedConvertible::CDiscretizedConvertible (QuantLib::Handle<QuantLib::DiscretizedConvertible> childNative)  : CDiscretizedAsset(CDiscretizedConvertible::typeid)
{
	_phDiscretizedConvertible = &childNative;
	_ppDiscretizedConvertible = &static_cast<boost::shared_ptr<QuantLib::DiscretizedConvertible>>(childNative.currentLink());
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedConvertible));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::CDiscretizedConvertible::CDiscretizedConvertible (QuantLib::DiscretizedConvertible childNative)  : CDiscretizedAsset(CDiscretizedConvertible::typeid)
{
#ifdef HANDLE
	_phDiscretizedConvertible = NULL;
#endif
	_ppDiscretizedConvertible = new boost::shared_ptr<QuantLib::DiscretizedConvertible> (new QuantLib::DiscretizedConvertible (childNative));
    _ppDiscretizedAsset = new boost::shared_ptr<QuantLib::DiscretizedAsset> (boost::dynamic_pointer_cast<QuantLib::DiscretizedAsset> (*_ppDiscretizedConvertible));
}
#endif

Cephei::QL::Experimental::CDiscretizedConvertible::~CDiscretizedConvertible ()
{
    if (_ppDiscretizedConvertible != NULL)
    {
	    delete _ppDiscretizedConvertible;
        _ppDiscretizedConvertible = NULL;
    }
}
Cephei::QL::Experimental::CDiscretizedConvertible::!CDiscretizedConvertible ()
{
    if (_ppDiscretizedConvertible != NULL)
    {
	    delete _ppDiscretizedConvertible;
    }
}
QuantLib::DiscretizedConvertible& Cephei::QL::Experimental::CDiscretizedConvertible::GetReference ()
{
    if (_ppDiscretizedConvertible == NULL) throw gcnew NativeNullException ();
	return **_ppDiscretizedConvertible;
}
boost::shared_ptr<QuantLib::DiscretizedConvertible>& Cephei::QL::Experimental::CDiscretizedConvertible::GetShared ()
{
    if (_ppDiscretizedConvertible == NULL) throw gcnew NativeNullException ();
	return *_ppDiscretizedConvertible;
}
QuantLib::DiscretizedConvertible* Cephei::QL::Experimental::CDiscretizedConvertible::GetPointer ()
{
    if (_ppDiscretizedConvertible == NULL) throw gcnew NativeNullException ();
	return &**_ppDiscretizedConvertible;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::DiscretizedConvertible>& Cephei::QL::Experimental::CDiscretizedConvertible::GetHandle ()
{
	if (_phDiscretizedConvertible == NULL)
	{
		_phDiscretizedConvertible = new Handle<QuantLib::DiscretizedConvertible> (*_ppDiscretizedConvertible);
	}
	return *_phDiscretizedConvertible;
}
#endif
bool Cephei::QL::Experimental::CDiscretizedConvertible::HasNative () 
{
	return (_ppDiscretizedConvertible != NULL);
}

Cephei::QL::Math::IArray^ Cephei::QL::Experimental::CDiscretizedConvertible::ConversionProbability::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppDiscretizedConvertible)->conversionProbability ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Experimental::CDiscretizedConvertible::DividendValues::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppDiscretizedConvertible)->dividendValues ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Experimental::CDiscretizedConvertible::MandatoryTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppDiscretizedConvertible)->mandatoryTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::IDiscretizedConvertible^ Cephei::QL::Experimental::CDiscretizedConvertible::Reset (UInt64 size)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _size = (QuantLib::Size)ValueHelper::Convert (size);
    	(*_ppDiscretizedConvertible)->reset ( _size );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Experimental::CDiscretizedConvertible::SpreadAdjustedRate::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppDiscretizedConvertible)->spreadAdjustedRate ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

